package com.qunar.qcf.core.filterchain;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qunar.qcf.core.filterchain.IoFilter.NextFilter;
import com.qunar.qcf.core.session.IdleStatus;
import com.qunar.qcf.core.session.IoEvent;
import com.qunar.qcf.core.session.IoEventType;
import com.qunar.qcf.core.session.IoSession;
import com.qunar.qcf.core.write.WriteRequest;

/**
 * An I/O event or an I/O request that MINA provides for {@link IoFilter}s.
 * Most users won't need to use this class.  It is usually used by internal
 * components to store I/O events.
 *
 */
public class IoFilterEvent extends IoEvent {
    /** A logger for this class */
    static Logger LOGGER = LoggerFactory.getLogger(IoFilterEvent.class);
    
    /** A speedup for logs */
    static boolean DEBUG = LOGGER.isDebugEnabled();

    private final NextFilter nextFilter;

    public IoFilterEvent(NextFilter nextFilter, IoEventType type,
            IoSession session, Object parameter) {
        super(type, session, parameter);

        if (nextFilter == null) {
            throw new IllegalArgumentException("nextFilter must not be null");
        }
        
        this.nextFilter = nextFilter;
    }

    public NextFilter getNextFilter() {
        return nextFilter;
    }

    @Override
    public void fire() {
        IoSession session = getSession();
        NextFilter nextFilter = getNextFilter();
        IoEventType type = getType();

        if (DEBUG) {
            LOGGER.debug( "Firing a {} event for session {}",type, session.getId() );
        }

        switch (type) {
        case MESSAGE_RECEIVED:
            Object parameter = getParameter();
            nextFilter.messageReceived(session, parameter);
            break;
            
        case MESSAGE_SENT:
            WriteRequest writeRequest = (WriteRequest)getParameter();
            nextFilter.messageSent(session, writeRequest);
            break;
            
        case WRITE:
            writeRequest = (WriteRequest)getParameter();
            nextFilter.filterWrite(session, writeRequest);
            break;
            
        case CLOSE:
            nextFilter.filterClose(session);
            break;
            
        case EXCEPTION_CAUGHT:
            Throwable throwable = (Throwable)getParameter();
            nextFilter.exceptionCaught(session, throwable);
            break;
            
        case SESSION_IDLE:
            nextFilter.sessionIdle(session, (IdleStatus) getParameter());
            break;
            
        case SESSION_OPENED:
            nextFilter.sessionOpened(session);
            break;
            
        case SESSION_CREATED:
            nextFilter.sessionCreated(session);
            break;
            
        case SESSION_CLOSED:
            nextFilter.sessionClosed(session);
            break;
            
        default:
            throw new IllegalArgumentException("Unknown event type: " + type);
        }
        
        if (DEBUG) {
            LOGGER.debug( "Event {} has been fired for session {}", type, session.getId() );
        }
    }
}
